package io.gitee.xyuesheng.day03;

import org.junit.Test;

public class TestExtends {
    @Test
    public void test() {
        Cat cat = new Cat();

        System.out.println(cat.id);

        cat.eat();
    }

    @Test
    public void testOverride() {
        Animal animal = new Animal();
        animal.shout();

        Cat cat = new Cat();
        cat.shout();

        System.out.println(animal.toString());
    }

    @Test
    public void testPolymorphism() {
        Boss boss = new Boss();

        Programmer p = new Programmer();

        boss.pay(p);

        Designer d = new Designer();
        boss.pay(d);


        // 多态: 一个基类的类型，可以引用不同的子类类型
        Employee employee = new Programmer();
        Employee employee2 = new Designer();

        employee.work();
        employee2.work();
    }

    @Test
    public void testPolymorphism2() {
        Employee employee = new Programmer();

        // work()方法，取决于对象的实际类型。跟声明的类型无关
        employee.work();

        // 子类特有的方法不可见
        //employee.overwork();

        // 向下转型
        Programmer programmer = (Programmer) employee;
        programmer.overwork();

        //
        // Designer designer = (Designer) employee;

        System.out.println(employee instanceof Designer);
        System.out.println(employee instanceof Programmer);
        System.out.println(employee instanceof Employee);
    }
}
